package com.mall.service.marketing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.model.domain.SmsCoupon;
import com.mall.model.dto.SmsCouponDto;
import com.mall.model.vo.PageResult;
import com.mall.service.marketing.mapper.SmsCouponMapper;
import com.mall.service.marketing.service.SmsCouponService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SmsCouponServiceImpl extends ServiceImpl<SmsCouponMapper, SmsCoupon> implements SmsCouponService {

    @Autowired
    private SmsCouponMapper smsCouponMapper;

    /**
     * 根据优惠券名称和类型分页获取优惠券列表
     *
     * @param name
     * @param type
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResult list(String name, Integer type, Integer pageNum, Integer pageSize) {
        IPage<SmsCoupon> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsCoupon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type != null, SmsCoupon::getType, type).like(StringUtils.isNotEmpty(name), SmsCoupon::getName, name);
        IPage<SmsCoupon> pageInfo = smsCouponMapper.selectPage(page, queryWrapper);
        return new PageResult(pageNum, pageSize, (int) pageInfo.getTotal(), pageInfo.getRecords());
    }

    /**
     * 添加优惠券
     *
     * @param smsCouponDto
     * @return
     */
    @Override
    public Integer create(SmsCouponDto smsCouponDto) {
        //添加优惠券基本信息
        int count = smsCouponMapper.insert(smsCouponDto);

        //插入优惠券和商品关系表

        //插入优惠券和商品分类关系表

        return count;
    }

    /**
     * 删除优惠券
     *
     * @param id
     * @return
     */
    @Override
    public Integer delete(Integer id) {
        //删除优惠券基本信息
        int count = smsCouponMapper.deleteById(id);
        //删除商品关联
        //删除商品分类关联
        return count;
    }

    /**
     * 修改优惠券
     *
     * @param id
     * @return
     */
    @Override
    public Integer update(Long id, SmsCouponDto smsCouponDto) {
        smsCouponDto.setId(id);
        //修改优惠券基本信息
        int count = smsCouponMapper.updateById(smsCouponDto);
        //删除修改商品关联
        //删除修改商品分类关联
        return count;
    }
}
